﻿using MediatR;
using SqlSugar;
using Warehouse.Domain;
using Warehouse.ErrorCord;
using Warehouse.Write.Api.Actions.Warehouse.Command;

namespace Warehouse.Write.Api.Actions.Warehouse.CommandHandle
{
    public class DelinformationCommandHandle : IRequestHandler<DelinformationCommand, APIResult<WarehouseInformation>>
    {
        private readonly ISqlSugarClient db;

        public DelinformationCommandHandle(ISqlSugarClient db)
        {
            this.db = db;
        }

        public async Task<APIResult<WarehouseInformation>> Handle(DelinformationCommand request, CancellationToken cancellationToken)
        {
            APIResult<WarehouseInformation> result = new APIResult<WarehouseInformation>();
            var res = await db.Queryable<WarehouseInformation>().ToListAsync();
            string[] ids = request.WarehouseId.Split(',');
            foreach (string id in ids)
            {
                var wh = res.Where(x => x.WarehouseId.Equals(Convert.ToInt32(id))).FirstOrDefault();
                if (wh != null && wh.WarehouseId != 0)
                {
                    wh.IsDel = !wh.IsDel;
                    await db.Updateable(wh).ExecuteCommandAsync();
                }
            }
            result.Code = APIEnum.Success;
            result.Msg = "删除成功";
            return await Task.FromResult(result);



        }
    }
}
